<template>
  <div>
    <div id="homelist" class="wikilist">
      <ul>
        <li v-for="item in list" v-bind:key="item.id">
          <div  @click="selectPage(item.id)">
            <a :href="'/p/' + item.id"></a>
            <p class="wiki-title">{{item.title}}</p>
            <p class="wiki-abstract">{{item.summary}}</p>
          </div>
          <div class="meta">
            <span>
              <i class="iconfont iconyanjing"></i>
              {{item.read_num}}
            </span>
            <span style="display: none" v-show="isLogin">
              <i class="iconfont iconbianji" @click="editePage(item.id)"></i>
            </span>
            <span style="display: none" v-show="isLogin">
              <i class="iconfont iconpicture-delet" @click="delPage(item.id)"></i>
            </span>
            <span>
             &nbsp;最后编辑于&nbsp;{{item.last_time}}
            </span>
          </div>
        </li>
      </ul>
      <div class="btngMore">
        <a class="btnMore" @click="getMore()">
          <span>加载更多</span>
        </a>
      </div>
      <a :href="'/?p=' + pagerNumer"></a>
    </div>
    <Rside class="rside"></Rside>
  </div>
</template>

<script>
import Rside from '~/components/Rside.vue'

export default {
  data () {
    return {
      isSelect: '首页',
      userName: '',
      // userName: $store.state.num,
      list: [],
      num: 2,
      loading: 0,
      noMore: 0,
      isLogin: null
    }
  },
  asyncData ({app,query}) {
    console.log(query)
    var regPos = /^[123456789]\d*$/
    let pager
    if(regPos.test(query.p)){
      pager = parseInt(query.p)
    }else{
      pager =  1
    }
    var qurl= 'wiki/getWikiList/' + pager
    console.log(qurl)
    if (process.client){
      qurl = process.env.BASE_URL + qurl
    }
    return app.$axios.get(qurl).then((res) => {
      // console.log(res)
      // this.list = res.data.data
      return{list: res.data.data,
        pagerNumer: pager + 1}
    }, function (response) {
      console.log('error')
    })
  },
  methods: {
    selectPage (wikiId) {
      console.log('run selectPage')
      this.$router.push('/p/' + wikiId)
    },
    getMore () {
      if (this.loading === 1) {
        return false
      }
      if (this.noMore === 1) {
        return false
      }
      this.loading = 1
      this.$axios.get(process.env.BASE_URL + 'wiki/getWikiList/' + this.num).then((res) => {
        // console.log(res)
        for (let i of res.data.data) {
          this.list.push(i)
        }
        this.loading = 0
        this.num += 1
        if (res.data.data.length === 0) {
          this.noMore = 1
        }
      }, function (response) {
        console.log('error')
      })
    }
  },
  components: {
    Rside
  }
}
</script>

<style scoped>
  #homelist {
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    text-align: center;
    color: #2c3e50;
    background: white;
    /*margin-top: 60px;*/
    display:inline-block;
    vertical-align:top;
    width: 64%;
    margin-right: 43px;
    box-shadow: 0 8px 17px 0 rgba(0,0,0,.12), 0 6px 20px 0 rgba(0,0,0,.14);
  }
  .wiki-title {
    /*margin: -7px 0 4px;*/
    display: inherit;
    font-size: 18px;
    font-weight: 700;
    line-height: 1.5;
  }
  .wiki-abstract {
    margin: 0 0 8px;
    font-size: 13px;
    line-height: 24px;
    color: #999;
  }
  .meta{
    font-size: 12px;
  }
  .btngMore {
    padding: 20px;
  }
  .btnMore {
    display:inline-block;
    font-family: 'Avenir', Helvetica, Arial, sans-serif;
    font-size: 16px;
    color: #8c8c8c;
    border: 1px solid hsla(0,0%,59%,.6);
    margin-right: auto;
    margin-left: auto;
    margin-top: 4px;
    padding: 8px 10px;
    min-width: 50%;
    border-radius: 40px;
    cursor: pointer;
  }
  .rside{
    display:inline-block;
    background: #fafafa;
    vertical-align:top;
    width: 25%;
  }
</style>
<style lang="less" scoped>
  .wikilist>ul{
    max-width: 85%;
    overflow: hidden;
    list-style:none;
    padding: 0 16px 0 72px;
    margin: 0 auto;
    li{
      min-height: 100px;
      text-align: left;
      padding: 15px 0 20px 0;
      border-bottom: 1px solid #f0f0f0;
      div p{
        cursor: pointer;
      }
      div span {
        color: #999;
        i{
          cursor: pointer;
        }
      }
    }
  }
</style>

